package com.javaProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaProject.dto.R;
import com.javaProject.entity.Xuanze;
import com.javaProject.service.XuanzeService;
import com.javaProject.mapper.XuanzeMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author ASUS
* @description 针对表【xuanze】的数据库操作Service实现
* @createDate 2024-03-19 17:41:19
*/
@Service
public class XuanzeServiceImpl extends ServiceImpl<XuanzeMapper, Xuanze>
    implements XuanzeService{


    @Override
    public R add(Xuanze xuanze) {
        QueryWrapper<Xuanze> query = Wrappers.query(Xuanze.class);
        query.eq("student_id",xuanze.getStudentId());
        Xuanze  one = getOne(query);
        if (Objects.nonNull(one)){
            if(update(one,query)){
                return R.ok().setMsg("修改成功");
            }
            return R.fail().setMsg("修改失败");
        }
        if (save(xuanze)){
            return R.ok().setMsg("新增成功");
        }
        return R.fail().setMsg("选择失败");
    }

    @Override
    public R Delect(Xuanze xuanze) {
        QueryWrapper<Xuanze> query = Wrappers.query(Xuanze.class);
        query.eq("bishe_id",xuanze.getBisheId());
        if (remove(query)){
           return R.ok();
        }
        return R.fail().setMsg("删除失败");
    }

    @Override
    public List<Xuanze> getStudentXuanze(Xuanze xuanze) {
        QueryWrapper<Xuanze> query = Wrappers.query(Xuanze.class);
        query.eq("bishe_id",xuanze.getBisheId());
        List<Xuanze> xuanzes = list(query);
        return xuanzes;
    }

    @Override
    public Integer getStudentBishi(Xuanze xuanze) {
        QueryWrapper<Xuanze> query = Wrappers.query(Xuanze.class);
        query.eq("student_id",xuanze.getStudentId());
        Xuanze xuanze1 = getOne(query);
        if (Objects.isNull(xuanze1)){
            return xuanze.getBisheId();
        }
        return xuanze1.getBisheId();

    }


}




